package com.wuliming.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wuliming.gulimall.product.entity.AttrEntity;
import com.wuliming.gulimall.product.entity.AttrGroupEntity;
import com.wuliming.gulimall.product.entity.vo.req.AttrGroupReqVo;
import com.wuliming.gulimall.product.entity.vo.resp.AttrGroupRespVo;
import com.wuliming.gulimall.product.service.AttrAttrgroupRelationService;
import com.wuliming.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuliming.gulimall.product.service.AttrGroupService;
import com.wuliming.common.utils.PageUtils;
import com.wuliming.common.utils.R;


/**
 * 属性分组
 *
 * @author wuliming
 * @email wuliming@gmail.com
 * @date 2023-12-05 12:11:06
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    //product/attrgroup/{catelogId}/withattr

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long categoryId) {
        List<AttrGroupRespVo> data = attrGroupService.getAttrGroupWithAttr(categoryId);
        return R.ok().put("data", data);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params,
                               @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.getNoAttrRelation(attrgroupId,params);
        return R.ok().put("page", page);
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrGroupService.getAttrRelation(attrgroupId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.getCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/attr/relation")
    public R save(@RequestBody AttrGroupReqVo[] attrGroup) {
        relationService.saveBatchs(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R del(@RequestBody AttrGroupReqVo[] reqVos) {
//        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrGroupService.removeBatch(reqVos);
        return R.ok();
    }


}
